import got from "got";
import { Article } from "../types/article";
import { readFileByType } from "./util";

const prefixUrl = process.env.ICAN_PREFIXURL;
const postArticleUrl = process.env.ICAN_POST_ARTICLE;
const token = readFileByType('access_token')

class ArtilceDTO {
  attachFile = ""
  attachFileList = []
  createUser = ""
  downloadCost = 0
  isCertified = 0
  isOpen = 1
  isOriginal = "0"
  materialContent = ""
  materialIntro = ""
  materialName = ""
  materialType = ""
  tag = []
}

const postArticle = async (id: string) => {
  const article: Article = readFileByType(`juejin/${id}`, 'json')
  const headers = {
    authorization: `Bearer ${token}`
  }
  const articleDto = new ArtilceDTO()
  articleDto.materialName = article.title
  articleDto.materialIntro = article.brief_content
  articleDto.materialContent = article.html
  articleDto.createUser = article.username

  const postUrl = `${prefixUrl}${postArticleUrl}`;
  try {
    const res = await got.post(postUrl, {
      method: "POST",
      headers,
      json: articleDto
    });
    return res;
  } catch (error) {
    console.error(error);
  }
};

export { postArticle };
